package vfs.client.command;

import vfs.client.Locale;
import vfs.common.command.remote.*;
import vfs.client.rmi.ClientRmiManager;
import vfs.common.Regex;
import vfs.common.command.*;

/**
 * User: serge
 * Date: 19.11.13
 * Time: 3:14
 */
public class ClientCommandFactory extends AbstractCommandFactory implements CommandFactory
{
    private static final ClientCommandFactory INSTANCE = new ClientCommandFactory();

    public static CommandFactory getInstance()
    {
        return INSTANCE;
    }

    @Override
    public Command getCommand(String call)
    {
        splitCall(call);

        if (Regex.stringMatchesMask(param0, vfs.common.Locale.CMD_QUIT, true))
        {
            return new QuitCommand();
        }
        else if (Regex.stringMatchesMask(param0, Locale.CMD_CONNECT, true))
        {
            ConnectCommand connectCommand = new ConnectCommand();
            connectCommand.setParams(params);
            return connectCommand;
        }
        else if (Regex.stringMatchesMask(param0, Locale.CMD_PRINT, true))
        {
            if (ClientRmiManager.getInstance().isSessionStarted())
            {
                RemoteCommand remoteCommand = new PrintCommand();
                remoteCommand.setUser(ClientRmiManager.getInstance().getUser());
                RemoteWrapperCommand wrapperCommand = new RemoteWrapperCommand(remoteCommand);
                return wrapperCommand;
            }
            else
            {
                return new RemoteNotAllowedCommand();
            }
        }
        else if (Regex.stringMatchesMask(param0, Locale.CMD_MD, true))
        {
            if (ClientRmiManager.getInstance().isSessionStarted())
            {
                RemoteCommand remoteCommand = new MakeDirCommand();
                remoteCommand.setParams(params);
                remoteCommand.setUser(ClientRmiManager.getInstance().getUser());
                RemoteWrapperCommand wrapperCommand = new RemoteWrapperCommand(remoteCommand);
                return wrapperCommand;
            }
            else
            {
                return new RemoteNotAllowedCommand();
            }
        }
        else if (Regex.stringMatchesMask(param0, Locale.CMD_MF, true))
        {
            if (ClientRmiManager.getInstance().isSessionStarted())
            {
                RemoteCommand remoteCommand = new MakeFileCommand();
                remoteCommand.setParams(params);
                remoteCommand.setUser(ClientRmiManager.getInstance().getUser());
                RemoteWrapperCommand wrapperCommand = new RemoteWrapperCommand(remoteCommand);
                return wrapperCommand;
            }
            else
            {
                return new RemoteNotAllowedCommand();
            }
        }
        else if (Regex.stringMatchesMask(param0, Locale.CMD_CD, true))
        {
            if (ClientRmiManager.getInstance().isSessionStarted())
            {
                RemoteCommand remoteCommand = new ChangeDirCommand();
                remoteCommand.setParams(params);
                remoteCommand.setUser(ClientRmiManager.getInstance().getUser());
                RemoteWrapperCommand wrapperCommand = new RemoteWrapperCommand(remoteCommand);
                return wrapperCommand;
            }
            else
            {
                return new RemoteNotAllowedCommand();
            }
        }
        else if (Regex.stringMatchesMask(param0, Locale.CMD_COPY, true))
        {
            if (ClientRmiManager.getInstance().isSessionStarted())
            {
                RemoteCommand remoteCommand = new CopyCommand();
                remoteCommand.setParams(params);
                remoteCommand.setUser(ClientRmiManager.getInstance().getUser());
                RemoteWrapperCommand wrapperCommand = new RemoteWrapperCommand(remoteCommand);
                return wrapperCommand;
            }
            else
            {
                return new RemoteNotAllowedCommand();
            }
        }
        else
        {
            return new UnknownCommand();
        }
    }
}
